import Vue from "vue";

import Vuex from "vuex";

import { longinAPI } from "@/api/login.js";

Vue.use(Vuex);

const store = new Vuex.Store({
  strict: true,
  state: {
    token: localStorage.getItem("token") || "",
    refresh_token: localStorage.getItem("refresh_token") || "",
    userPhoto:
      "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.616pic.com%2Fys_bnew_img%2F00%2F10%2F83%2F54xXAJVRIn.jpg&refer=http%3A%2F%2Fpic.616pic.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1657068626&t=a0844a3994fd4a405f1190e4476541a5",
  },
  mutations: {
    setToken(state, newToken) {
      state.token = newToken;
      localStorage.setItem("token", newToken);
    },
    setRefresh_token(state, newRefresh_token) {
      state.refresh_token = newRefresh_token;
      localStorage.setItem("refresh_token", newRefresh_token);
    },
    removeToken(state) {
      state.token = "";
      localStorage.setItem("token", "");
    },
    // getToken(state) {
    //   return state.token;
    // },
    // 直接可以this.$store.token获取
    setUserPhoto(state, value) {
      state.userPhoto = value;
    },
  },
  actions: {
    async asyncSetToken(store, fromLogin) {
      const resp = await longinAPI(fromLogin);
      // console.log(resp);
      const { refresh_token: refresh_token, token: token } = resp.data.data;
      store.commit("setToken", token);
      // console.log(token);
      store.commit("setRefresh_token", refresh_token);
      console.log(refresh_token);
    },
  },
  getters: {},
  modules: {},
});

export default store;
